package com.bwda.dsrs.filecenter.controller;


import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.aspect.LogTypeEnum;
import com.bwda.dsrs.filecenter.aspect.Loggable;
import com.bwda.dsrs.filecenter.domain.condition.clientappversion.ClientAppVersionCondition;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.form.clientappversion.ClientAppVersionCreateForm;
import com.bwda.dsrs.filecenter.domain.form.clientappversion.ClientAppVersionQueryForm;
import com.bwda.dsrs.filecenter.domain.form.clientappversion.ClientAppVersionUpdateForm;
import com.bwda.dsrs.filecenter.domain.po.clientappversion.ClientAppVersionPo;
import com.bwda.dsrs.filecenter.domain.vo.clientappversion.ClientAppVersionVo;
import com.bwda.dsrs.filecenter.service.ClientAppVersionService;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.form.datadict.DatadictQueryForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 客户端版本
 *
 * @author yangjun
 */
@RestController
@RequestMapping("/b/clientappversion")
@Api(value = "/b/clientappversion", tags = "客户端版本")
public class ClientAppVersionController extends BaseController {

    @Autowired
    private ClientAppVersionService clientAppVersionService;

    @ApiOperation(value = "查询客户端版本", notes = "查询客户端版本", httpMethod = "GET")
    @Loggable(descp = "查询客户端版本", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<ClientAppVersionVo> query(@ApiParam(value = "ID", required = true) @RequestParam Integer id) throws BusinessServiceException {
        ClientAppVersionPo po = clientAppVersionService.queryWithValid(id);
        ClientAppVersionVo vo = CopyUtil.transfer(po, ClientAppVersionVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "查询客户端版本数量", notes = "查询客户端版本数量", httpMethod = "GET")
    @Loggable(descp = "查询客户端版本数量", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<Integer> queryCount(@ModelAttribute @Valid ClientAppVersionQueryForm form) throws BusinessServiceException {
        ClientAppVersionCondition condition = this.getConditionByQueryForm(form);
        int count = clientAppVersionService.queryCount(condition);
        return getSuccessResult(count);
    }

    @ApiOperation(value = "查询客户端版本列表", notes = "查询客户端版本列表", httpMethod = "GET")
    @Loggable(descp = "查询客户端版本列表", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreListResponse<ClientAppVersionVo>> queryList(@ModelAttribute @Valid ClientAppVersionQueryForm form) throws BusinessServiceException, SystemServiceException {
        ClientAppVersionCondition condition = this.getConditionByQueryForm(form);
        List<ClientAppVersionPo> poList = clientAppVersionService.queryList(condition);
        List<ClientAppVersionVo> voList = CopyUtil.transfer(poList, ClientAppVersionVo.class);
        return getSuccessResult(getListResponse(voList));
    }

    @ApiOperation(value = "查询客户端版本列表(带分页)", notes = "查询客户端版本列表(带分页)", httpMethod = "GET")
    @Loggable(descp = "查询客户端版本列表(带分页)", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreCutPageResponse<ClientAppVersionVo>> queryPageList(@ModelAttribute @Valid ClientAppVersionQueryForm form) throws BusinessServiceException {
        ClientAppVersionCondition condition = this.getConditionByQueryForm(form);
        List<ClientAppVersionVo> voList = new ArrayList<>();
        int count = clientAppVersionService.queryCount(condition);
        if (count > 0) {
            List<ClientAppVersionPo> poList = clientAppVersionService.queryList(condition);
            voList = CopyUtil.transfer(poList, ClientAppVersionVo.class);
        }
        return getSuccessResult(getPageResponse(form, count, voList));
    }

    @ApiOperation(value = "新增客户端版本", notes = "新增客户端版本", httpMethod = "POST")
    @Loggable(descp = "新增客户端版本", type = LogTypeEnum.CREATE, include = "")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<ClientAppVersionVo> add(@ModelAttribute @Valid ClientAppVersionCreateForm form) throws BusinessServiceException {
        ClientAppVersionPo po = CopyUtil.transfer(form, ClientAppVersionPo.class);
        po.setId(1);
        clientAppVersionService.insert(po);
        ClientAppVersionVo vo = CopyUtil.transfer(po, ClientAppVersionVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "修改客户端版本", notes = "修改客户端版本", httpMethod = "POST")
    @Loggable(descp = "修改客户端版本", type = LogTypeEnum.UPDATE, include = "")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid ClientAppVersionUpdateForm form) throws BusinessServiceException {
        ClientAppVersionPo po = CopyUtil.transfer(form, ClientAppVersionPo.class);
        clientAppVersionService.update(po);
        return getSuccessResult();
    }

    @ApiOperation(value = "删除客户端版本", notes = "删除客户端版本", httpMethod = "POST")
    @Loggable(descp = "删除客户端版本", type = LogTypeEnum.DELETE, include = "")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity delete(@ApiParam(value = "ID", required = true) @RequestParam Integer id) throws BusinessServiceException {
        clientAppVersionService.delete(id);
        return getSuccessResult();
    }

    /**
     * ClientAppVersionQueryForm转换为ClientAppVersionCondition
     *
     * @param form
     * @return
     */
    private ClientAppVersionCondition getConditionByQueryForm(ClientAppVersionQueryForm form) {
        ClientAppVersionCondition condition = CopyUtil.transfer(form, ClientAppVersionCondition.class);
        return condition;
    }

}